﻿using AntUnion.FrameWork.Model.Models;
using AntUnion.FrameWork.Web.IService;
using Microsoft.EntityFrameworkCore;
using System.Linq;

namespace AntUnion.FrameWork.Web.Service
{
    public class RoleService : IRoleService
    {
       /// <summary>
       /// 通过用户编号获取角色
       /// </summary>
       /// <param name="userId"></param>
       /// <returns></returns>
        public TbRole GetRole(int userId, UserType userType)
        {
            try
            {
                using (mtContext mtContext = new mtContext())
                {
                    //获取对应的角色编号
                    TbUserRole userRole = mtContext.TbUserRoles.Where(k => k.UserId == userId && k.UserType == (int)userType).FirstOrDefault();
                    //通过角色编号获取角色对象
                    if (userRole != null)
                    {
                        TbRole role = mtContext.TbRoles.Where(k => k.Id == userRole.RoleId && k.UserType == (int)userType).FirstOrDefault();
                        return role;
                    }
                    else
                    {
                        throw new System.Exception(ExpectionService.userRoleNull);
                    }
                }
            }
            catch (System.Exception)
            {
                throw;
            }
        }
    }
}
